﻿using System;
using System.Collections.Generic;
using System.Linq;
using WEB.BLL.Entities;
using WEB.BLL.Services;
using Ext.Net;
using WEB.Core.Util;
using WEB.Core.Controls;

namespace WEB.APP.Controls.List
{
    public partial class StudentForm : BaseControl
    {
        public event EventHandler SaveClick;
        private void OnSaveClick()
        {
            if (SaveClick != null)
            {
                SaveClick(this, EventArgs.Empty);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected override void CheckPer(bool Edit)
        {
            base.CheckPer(Edit);
            nfStudentId.Disabled = Edit;
        }

        public void Show(string StudentId)
        {
            var StudentE = new StudentEntity() { StudentId = Convert.ToDecimal(StudentId) };
            StudentE = StudentService.GetInstance().LoadByPrimaryKey(StudentE);
            if (null == StudentE)
                return;
            FormPanel1.Reset();
            nfStudentId.Value = StudentE.StudentId;
            hfStudentId.Text = StudentE.StudentId.ToString();
            txtLocationId.Value = StudentE.LocationId;
            cboSex.SelectedIndex = StudentE.Sex == true ? 0 : 1;
            dfBirthDt.SelectedDate = StudentE.BirthDt.Value;
            txtHomeLang.Text = StudentE.HomeLang;
            txtSurName.Text = StudentE.SurName;
            txtGivenName.Text = StudentE.GivenName;
            nfCourseId.Value = StudentE.CourseId;
            txtCourseTitle.Text = StudentE.CourseTitle;
            nfComYr.Value = StudentE.ComYr;
            nfUaiYr.Value = StudentE.UaiYr;
            nfUai.Value = StudentE.Uai;
            nfGpa.Value = StudentE.Gpa;
            nfCreditPass.Value = StudentE.CreditPass;
            txtEmailAddr.Text = StudentE.EmailAddr;
            txtAddr1.Text = StudentE.Addr1;
            txtAddr2.Text = StudentE.Addr2;
            txtAddr3.Text = StudentE.Addr3;
            txtAddr4.Text = StudentE.Addr4;
            txtAddr5.Text = StudentE.Addr5;
            txtHomePhone.Text = StudentE.HomePhone;
            txtWorkPhone.Text = StudentE.WorkPhone;
            txtMobile.Text = StudentE.Mobile;
            nfAustPostcode.Value = StudentE.AustPostcode;
            txtCitizenship.Text = StudentE.Citizenship;
            txtCitizenshipStatus.Text = StudentE.CitizenshipStatus;
            if (StudentE.ProjectId != null)
                cbProject.Value = StudentE.ProjectId;
            CheckPer(true);
            winDetails.Show();
        }
        public void Show()
        {
            hfStudentId.Text = string.Empty;
            FormPanel1.Reset();
            CheckPer(false);
            winDetails.Show();
        }

        protected void btnNew_Click(object sender, DirectEventArgs e)
        {
            FormPanel1.Reset();
            CheckPer(false);
        }

        protected bool CheckInput()
        {
            if (string.IsNullOrEmpty(nfStudentId.Text.Trim()) || nfStudentId.Text.Trim().Length < 10)
            {
                X.Msg.Alert("Informational", "Student Id not valid!").Show();
                return false;
            }
            if (string.IsNullOrEmpty(hfStudentId.Text))
            {
                if (StudentService.GetInstance().LoadByPrimaryKey(new StudentEntity() { StudentId = Convert.ToDecimal(nfStudentId.Value) }) != null)
                {
                    X.Msg.Alert("Error", "Student Id is already existing").Show();
                    return false;
                }
            }
            if (string.IsNullOrEmpty(cbProject.Value.ToString()) && ProjectService.GetInstance().LoadPrimaryKey(new ProjectEntity() { ProjectId = Convert.ToDecimal(cbProject.Value) }) == null)
            {
                X.Msg.Alert("Error", "ProjectId Id isn't existing").Show();
                return false;
            }
            return true;
        }

        protected StudentEntity GetValueInEntity()
        {
            var StudentE = new StudentEntity()
            {
                StudentId = Convert.ToDecimal(nfStudentId.Value),
                LocationId = txtLocationId.Value.ToString(),
                Sex = cboSex.SelectedIndex == 0 ? true : false,
                BirthDt = dfBirthDt.SelectedDate,
                HomeLang = txtHomeLang.Text.Trim(),
                SurName = txtSurName.Text.Trim(),
                GivenName = txtGivenName.Text.Trim(),
                CourseId = Convert.ToDecimal(nfCourseId.Value),
                CourseTitle = txtCourseTitle.Text.Trim(),
                ComYr = Convert.ToDecimal(nfComYr.Value),
                UaiYr = Convert.ToDecimal(nfUaiYr.Value),
                Uai = Convert.ToDecimal(nfUai.Value),
                Gpa = Convert.ToDecimal(nfGpa.Value),
                CreditPass = Convert.ToDecimal(nfCreditPass.Value),
                EmailAddr = txtEmailAddr.Text.Trim(),
                Addr1 = txtAddr1.Text.Trim(),
                AustPostcode = Convert.ToDecimal(nfAustPostcode.Value),
                Citizenship = txtCitizenship.Text.Trim(),
                CitizenshipStatus = txtCitizenshipStatus.Text.Trim()
            };
            if (!string.IsNullOrEmpty(txtAddr2.Text))
            {
                StudentE.Addr2 = txtAddr2.Text.Trim();
            }
            if (!string.IsNullOrEmpty(txtAddr3.Text))
            {
                StudentE.Addr3 = txtAddr2.Text.Trim();
            }
            if (!string.IsNullOrEmpty(txtAddr4.Text))
            {
                StudentE.Addr4 = txtAddr2.Text.Trim();
            }
            if (!string.IsNullOrEmpty(txtAddr5.Text))
            {
                StudentE.Addr5 = txtAddr2.Text.Trim();
            }
            if (!string.IsNullOrEmpty(txtHomePhone.Text))
            {
                StudentE.HomePhone = txtHomePhone.Text.Trim();
            }
            if (!string.IsNullOrEmpty(txtWorkPhone.Text))
            {
                StudentE.WorkPhone = txtWorkPhone.Text.Trim();
            }
            if (!string.IsNullOrEmpty(txtMobile.Text))
            {
                StudentE.Mobile = txtMobile.Text.Trim();
            }
            if (cbProject.SelectedIndex > 0)
                StudentE.ProjectId = Convert.ToDecimal(cbProject.Value);
            return StudentE;
        }

        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            try
            {
                var StudentE = GetValueInEntity();
                var StudentS = StudentService.GetInstance();
                if (!string.IsNullOrEmpty(hfStudentId.Text))
                {
                    //StudentE.StudentId = Convert.ToDecimal(hfStudentId.Text);
                    StudentS.Update(StudentE);
                }
                else
                {
                    if (StudentS.Insert(StudentE) != 1)
                    {
                        X.Msg.Alert("Error", "Cann't insert data, please try again later.").Show();
                        return;
                    }
                    hfStudentId.Text = StudentE.StudentId.ToString();
                }
                CheckPer(true);
                //winDetails.Hide();
                OnSaveClick();

            }
            catch (Exception ex)
            {
                Notification(ex);
            }
        }
    }
}